#/usr/bin/python
# coding: utf8

import time
from  PAM30 import PAM30
import sys
import win32api
import win32con

reload(sys)
sys.setdefaultencoding("utf-8")


def ordd(str):
    '''
    解决键盘码不对应问题,不知道别的电脑有没有这个问题
    '''
    t = ord(str)
    #65到90是A到Z
    if(t>90):
        return t-32
    else:
        return t


def typeme(passw):
    '''
    模拟人工输入,自动输入参数字符串到焦点窗口
    '''
    psht = len(passw)
    for j in range(psht):
        win32api.keybd_event(ordd(passw[j]), 0, 0, 0)
        time.sleep(0.5)
        win32api.keybd_event(ordd(passw[j]), 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(0.5)


def lgTaobao(username, userpass):
    '''
    使用指定用户名密码（在IE）登录淘宝
    '''
    #lgurl = 'https://login.taobao.com/member/login.jhtml?spm=2013.1.0.2'
    lgurl = 'https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Fi.taobao.com%2Fmy_taobao.htm%3Fspm%3D1.1000386.0.10'
    ie.navigate(lgurl)
    ie.setTextBox('TPL_username', username.decode('utf-8'))
    ie.setCheckBox('J_SafeLoginCheck', 0)
    bt = ie.findElement('button', 'id', 'J_SubmitStatic')
    #ie.clickButton('J_SubmitStatic')
    ie.clickElement(bt)
    ie.setTextBox('TPL_password', userpass)
    typeme(userpass)
    time.sleep(0.5)
    bt = ie.findElement('button', 'id', 'J_SubmitStatic')
    ie.clickElement(bt)
    if(ie.findText(username)):
        return True
    else:
        return False


def clickfav():
    '''
    点击收藏按钮，默认是登录状态
    '''
    bar = ie.findElement('span', 'innerText', u'收藏宝贝')
    ie.clickElement(bar)
    time.sleep(1)
    x = ie.findElement('a', 'innerText', 'X')
    ie.clickElement(x)



ie= PAM30.PAMIE()

lgTaobao('长胡子的小羊', 'tq3jx8')
time.sleep(2)
ie.navigate('http://item.taobao.com/item.htm?spm=2013.1.13.2&id=19265488343')  

time.sleep(2)
clickfav()
